package com.mgface.zookeeper;

import java.time.Duration;
import java.time.Instant;
import java.util.concurrent.CountDownLatch;

import org.apache.log4j.Logger;
import org.apache.zookeeper.Watcher.Event.KeeperState;
import org.apache.zookeeper.ZooKeeper;

public class DeleteZnode {
	private static Logger log= Logger.getLogger(DeleteZnode.class);
	public static void main(String[] args) throws Exception {
		CountDownLatch cdl = new CountDownLatch(1);
		ZooKeeper zk = new ZooKeeper("192.168.56.101:2182", 2_000, e->{
			log.info("接收到的event:"+e);
			if(e.getState()==KeeperState.SyncConnected){
				cdl.countDown();
			}
		});
		log.info("ZK状态:"+zk.getState().toString());
		Instant start = Instant.now();
		cdl.await();
		Instant end = Instant.now();
		Duration duration = Duration.between(start, end);
		log.info("花费时间:"+duration.toMillis()+"ms");
		log.info("zookeeper session established");
		//删除节点
		//zk.delete("/zk/002", 0);
		//异步删除节点
		CountDownLatch cdl2 = new CountDownLatch(1);
		zk.delete("/zk/002", 0, (rc,_path,ctx)->{
			log.info("删除节点:["+rc+","+_path+","+ctx+"]");
			cdl2.countDown();
		}, "i am context");
		cdl2.await();
	}

}
